package com.firefly.dao.configure;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Ticker;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @description: caffeine 缓存
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2023/4/26
 **/
//@Configuration
@Data
@ConfigurationProperties("cache-config")
public class CaffeineCacheConfig {
    private Map<String, CacheSpec> specs;
    @Data
    public static class CacheSpec {
        private Integer expireTime;
        private Integer maxSize;
        private Integer initSize;
    }
    @Bean
    public CacheManager cacheManager(Ticker ticker) {
        SimpleCacheManager manager = new SimpleCacheManager();
        if (specs != null) {
            List<CaffeineCache> caches = specs.entrySet().stream()
                                            .map(entry -> buildCache(entry.getKey(),entry.getValue(),ticker))
                                            .collect(Collectors.toList());
            manager.setCaches(caches);
        }
        return manager;
    }

    private CaffeineCache buildCache(String name, CaffeineCacheConfig.CacheSpec cacheSpec, Ticker ticker) {
        final Caffeine<Object, Object> caffeineBuilder = Caffeine.newBuilder()
                                                            // 设置最后一次写入或访问后经过固定时间过期
                                                            .expireAfterWrite(cacheSpec.getExpireTime(), TimeUnit.SECONDS)
                                                            // 缓存的最大条数
                                                            .maximumSize(cacheSpec.getMaxSize())
                                                            // 初始的缓存空间大小 M
                                                            .initialCapacity(cacheSpec.getInitSize())
                                                            .ticker(ticker);
        return new CaffeineCache(name,caffeineBuilder.build());
    }
    @Bean
    public Ticker ticker() {
        return Ticker.systemTicker();
    }
    /**
     * 配置缓存管理器
     *
     * @return 缓存管理器
     */
    /*@Bean("caffeineCacheManager")
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                // 设置最后一次写入或访问后经过固定时间过期
                .expireAfterWrite(7200, TimeUnit.SECONDS)
                // 初始的缓存空间大小
                .initialCapacity(100)
                // 缓存的最大条数
                .maximumSize(1000));
        return cacheManager;
    }*/
}
